package com.F01.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

import com.F01.actionForm.F010101ActionForm;
import com.F01.actionForm.F010103ActionForm;
import com.config.Constants;
import com.dao.common.DbOperationExt;
import com.dbtools.Dispose;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;

/**
 * @wangk071226
 * @成品库存管理
 * @数据库操作类
 * @方法命名规则：静态参数名称、例如：查询方法，方法名 QUERYLIST();
 * @数据库某字段如果的新增值，如果要取当前时间，请在数据库中表时间的字段上设置GETDATE(), @ insert语句执行时，会自动插入时间。避免系统时间于服务器时间不统一造成错误。 @ 例表c_customers的customer_date字段。
 */
public class F010103DAO extends DbOperationExt {

	/**
	 * 记录操作日志;
	 */
	Logger log = Logger.getLogger(F010103DAO.class);

	/**
	 * 构造方法获取数据源;
	 * 
	 */
	public F010103DAO() {
		super();
	}

	public F010103DAO(Connection conn) {
		super(conn);
	}

	/**
	 * @查询
	 */
	public List QUERYLIST(F010103ActionForm actionForm,
			HttpServletRequest request) {
		StringBuffer msql = new StringBuffer();
		StringBuffer condition = new StringBuffer();
		/** 判断查询条件，拼装查询SQL */
		/** 订单号码 */
		if (actionForm.getCotract_number() != null
				&& !actionForm.getCotract_number().equals("")) {
			condition.append(" and b.cotract_number = '"
					+ actionForm.getCotract_number().trim() + "' ");
		}
		/** 工 作 号 */
		if (actionForm.getManufacturing_order() != null
				&& !actionForm.getManufacturing_order().equals("")) {
			condition.append(" and a.manufacturing_order = '"
					+ actionForm.getManufacturing_order().trim() + "' ");
		}
		/** 入库时间 */
		if ((actionForm.getStock_date_begin() != null && !"".equals(actionForm
				.getStock_date_begin()))
				|| (actionForm.getStock_date_end() != null && !""
						.equals(actionForm.getStock_date_end()))) {

			if (!"".equals(actionForm.getStock_date_begin())
					&& !"".equals(actionForm.getStock_date_end())) {
				condition.append(" and stock_date BETWEEN '"
						+ actionForm.getStock_date_begin().trim() + "'");
				condition.append(" and '"
						+ actionForm.getStock_date_end().trim() + "' ");
			}
			if (!"".equals(actionForm.getStock_date_begin())
					&& "".equals(actionForm.getStock_date_end())) {
				condition.append(" and stock_date >='"
						+ actionForm.getStock_date_begin().trim() + "'");
			}
			if ("".equals(actionForm.getStock_date_begin())
					&& !"".equals(actionForm.getStock_date_end())) {
				condition.append(" and stock_date >='"
						+ actionForm.getStock_date_end() + "'");
			}
		}
		List list = new ArrayList();
		Map map = null;
		ResultSet rest = null;

		msql.append(" select ");
		msql.append(" b.cotract_number as cotract_number,a.manufacturing_order as manufacturing_order,");
		msql.append(" adyh.FUN_C_CUSTOMERS(b.customer_code) as customer_code,a.groups as groups,a.qty as qty,");
		msql.append(" a.stock_date as stock_date,b.unit_price as unit_price, ");
		msql.append(" convert(float,b.unit_price)*a.qty as selltotalprice,c.material_cost as material_cost,");
		msql.append(" convert(float,c.material_cost)*a.qty as costtotalprice,a.stock_reason as stock_reason ");
		msql.append(" from pd_finish_product a ");
		msql.append(" left join c_orders b on ");
		msql.append(" a.manufacturing_order=b.manufacturing_order ");
		msql.append(" left join t_not_standard_products c  ");
		msql.append(" on a.manufacturing_order=c.manufacturing_order ");
		msql.append(" where 1=1 " + condition.toString() + " ");
		msql.append(" order by  manufacturing_order ");
		try {
			log.info("成品库存管理 QUERYLIST sql is " + msql.toString());
			rest = super.executeQuery(msql.toString());
			while (rest.next()) { // 循环结果集并在map中添加结果集每行对应字段的值;
				map = new HashMap();
				map.put("cotract_number", CommonTools.null2String(rest
						.getString("cotract_number")));
				map.put("manufacturing_order", CommonTools.null2String(rest
						.getString("manufacturing_order")));
				map.put("customer_code", CommonTools.null2String(rest
						.getString("customer_code")));
				map.put("group", CommonTools.null2String(rest
						.getString("groups")));
				map.put("qty", CommonTools.null2String(rest.getString("qty")));
				map.put("stock_date", CommonTools.getDateTime(rest
						.getDate("stock_date")));
				map.put("unit_price", CommonTools.convertCurrencyRMB(CommonTools.null2String(rest
						.getString("unit_price"))));
				map.put("selltotalprice", CommonTools.convertCurrencyRMB(CommonTools.null2String(rest
						.getString("selltotalprice"))));
				map.put("material_cost", CommonTools.convertCurrencyRMB(CommonTools.null2String(rest
						.getString("material_cost"))));
				map.put("costtotalprice", CommonTools.convertCurrencyRMB(CommonTools.null2String(rest
						.getString("costtotalprice"))));
				map.put("stock_reason", CommonTools.null2String(rest
						.getString("stock_reason")));
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}

		return list;
	}

	/**
	 * @编辑保存
	 */
	public boolean SAVE_UPDATE(String manufacturing_order, String group,
			String stock_reason, T_S_YHCZXXinfo t_YhczxxInfo)
			throws SelectException {
		boolean flag = false;
		List sqlList = new ArrayList();
		StringBuffer yhczsql = new StringBuffer();
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);
		StringBuffer msql = new StringBuffer();
		msql.append(" UPDATE pd_finish_product  SET ");
		msql.append("   stock_reason = '" + stock_reason + "'");
		msql.append("   where manufacturing_order='" + manufacturing_order
				+ "' and ");
		msql.append("   groups='" + group + "'");
		try {
			log.info("成品库存管理 SAVE_UPDATE sql is " + msql.toString());
			sqlList.add(msql);
			sqlList.add(yhczsql);
			flag = super.executeUpdate(sqlList, true);
		} catch (Exception e) {
			e.printStackTrace();
			super.rollback();

		}
		super.commit();
		return flag;
	}

}
